package Database;

import Entity.LoanType;
import Entity.Role;
import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.Types;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author CONG
 */
public class LoanTypeDB {
    
    private ConnectDB conn;
    
    public LoanTypeDB() {
        conn = new ConnectDB();
    }
    
    public ArrayList<LoanType> getLoanTypeList() {
        ArrayList<LoanType> listLoanType = new ArrayList<LoanType>();
        try {
            CallableStatement cstmt = conn.connect().prepareCall("{call getLoanTypeList}");
            ResultSet rs = cstmt.executeQuery();
            while (rs.next()) {
                int id = rs.getInt(1);
                String name = rs.getString(2);
                String description = rs.getString(3);
                int rate = rs.getInt(4);
                boolean status = rs.getBoolean(5);
                LoanType lt = new LoanType(id, name, description, rate, status);
                listLoanType.add(lt);
            }
        } catch (Exception ex) {
            Logger.getLogger(LoanTypeDB.class.getName()).log(Level.SEVERE, null, ex);
        }
        return listLoanType;
    }
    
    public LoanType addLoanType(LoanType lt) {
        try {
            CallableStatement cstmt = conn.connect().prepareCall("{call insertLoanType(?,?,?,?,?)}");
            cstmt.registerOutParameter("LoanTypeCode", Types.INTEGER);
            cstmt.setString("Name", lt.getName());
            cstmt.setString("Description", lt.getDescription());
            cstmt.setFloat("InterestRate", lt.getInterestRate());
            cstmt.setBoolean("Status", lt.isStatus());
            cstmt.executeUpdate();
            int loanTypeCode = cstmt.getInt("LoanTypeCode");
            lt.setLoanTypeCode(loanTypeCode);
        } catch (Exception ex) {
            Logger.getLogger(LoanTypeDB.class.getName()).log(Level.SEVERE, null, ex);
        }
        return lt;
    }
    
    public void updateLoanType(LoanType lt) {
        try {
            CallableStatement cstmt = conn.connect().prepareCall("{call updateLoanType(?,?,?,?,?)}");
            cstmt.setInt("LoanTypeCode", lt.getLoanTypeCode());
            cstmt.setString("Name", lt.getName());
            cstmt.setString("Description", lt.getDescription());
            cstmt.setFloat("InterestRate", lt.getInterestRate());
            cstmt.setBoolean("Status", lt.isStatus());
            cstmt.executeUpdate();
        } catch (Exception ex) {
            Logger.getLogger(LoanTypeDB.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    
    public void deleteLoanType(LoanType lt) {
        try {
            CallableStatement cstmt = conn.connect().prepareCall("{call deleteLoanType(?)}");
            cstmt.setInt("LoanTypeCode", lt.getLoanTypeCode());
            cstmt.executeUpdate();
        } catch (Exception ex) {
            Logger.getLogger(LoanTypeDB.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    
    public boolean checkLoanType(String name) {
        try {
            CallableStatement cstmt = conn.connect().prepareCall("{call checkLoanType(?)}");
            cstmt.setString("name", name);
            ResultSet rs = cstmt.executeQuery();
            return rs.next();
        } catch (Exception ex) {
            Logger.getLogger(LoanTypeDB.class.getName()).log(Level.SEVERE, null, ex);
            return false;
        }
    }
}
